package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/testlist")
    public void testlist(){
        List<Region> list = regionService.list();
        list.forEach(System.out::println);
    }

    //带分页的查询
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") RegionQuery qo, Model model){
        Page<Region> page = regionService.query(qo);
        model.addAttribute("page", page);
        return "region/list";
    }

    @RequestMapping("/listDests")
    @ResponseBody
    public List<Destination> listDests(){
        //调用目的地service的list方法，查询所有目的
        List<Destination> list = destinationService.list();
        return list;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Region region){
        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public List<Destination> getDestByRegionId(String rid){
        List<Destination> list = destinationService.getDestByRegionId(rid);
        System.out.println(list.size());
        return list;
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public JsonResult changeHotValue(String id, Integer hot){
        regionService.changeHotValue(id, hot);
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }



}
